package hdr_plugin;

import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;
import ij.WindowManager;

/*
 * HDRStackBuilderFrame.java
 *
 * Created on 7. November 2008, 09:56
 */
import ij.text.TextWindow;

/**
 *
 * @author Alexander Heidrich
 */
public class HDRStackBuilderFrame extends java.awt.Frame {

    /** Creates new form HDRStackBuilderFrame */
    public HDRStackBuilderFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        lblLogo = new javax.swing.JLabel();
        panel1 = new java.awt.Panel();
        lblImgOpen = new java.awt.Label();
        listImgOpen = new java.awt.List();
        panel2 = new java.awt.Panel();
        btnMvRightAll = new java.awt.Button();
        btnMvRight = new java.awt.Button();
        btnMvLeft = new java.awt.Button();
        btnMvLeftAll = new java.awt.Button();
        panel3 = new java.awt.Panel();
        lblImgSelected = new java.awt.Label();
        listImgSelected = new java.awt.List();
        panel4 = new java.awt.Panel();
        btnMvTop = new java.awt.Button();
        btnMvUp = new java.awt.Button();
        btnMvDown = new java.awt.Button();
        btnMvBottom = new java.awt.Button();
        panel5 = new java.awt.Panel();
        btnCreateStack = new java.awt.Button();
        btnCancel = new java.awt.Button();
        btnHelp = new java.awt.Button();

        setBackground(java.awt.Color.white);
        setResizable(false);
        setTitle("HDR Stack Builder");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });
        setLayout(new java.awt.GridBagLayout());

        lblLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/hdr_plugin/resources/log.jpg"))); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 0);
        add(lblLogo, gridBagConstraints);

        panel1.setLayout(new java.awt.GridBagLayout());

        lblImgOpen.setText("Currently Opened Items");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        panel1.add(lblImgOpen, gridBagConstraints);

        listImgOpen.setMultipleMode(true);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 200;
        gridBagConstraints.ipady = 50;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        panel1.add(listImgOpen, gridBagConstraints);
        int[] imgList = WindowManager.getIDList();

        String[] imgTitles = new String[imgList.length];
        for (int i = 0; i < imgList.length; i++) {
            ImagePlus imp = WindowManager.getImage(imgList[i]);
            if (imp != null) {
                imgTitles[i] = imp.getTitle();
            } else {
                imgTitles[i] = "";
            }
        }

        for (String title : imgTitles) {
            listImgOpen.add(title);
        }

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        add(panel1, gridBagConstraints);

        panel2.setLayout(new java.awt.GridBagLayout());

        btnMvRightAll.setLabel(">>");
        btnMvRightAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMvRightAllActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        panel2.add(btnMvRightAll, gridBagConstraints);

        btnMvRight.setLabel(">");
        btnMvRight.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMvRightActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        panel2.add(btnMvRight, gridBagConstraints);

        btnMvLeft.setEnabled(false);
        btnMvLeft.setLabel("<");
        btnMvLeft.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMvLeftActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        panel2.add(btnMvLeft, gridBagConstraints);

        btnMvLeftAll.setEnabled(false);
        btnMvLeftAll.setLabel("<<");
        btnMvLeftAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMvLeftAllActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        panel2.add(btnMvLeftAll, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        add(panel2, gridBagConstraints);

        panel3.setLayout(new java.awt.GridBagLayout());

        lblImgSelected.setText("Currently Selected Items");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        panel3.add(lblImgSelected, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 200;
        gridBagConstraints.ipady = 50;
        panel3.add(listImgSelected, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        add(panel3, gridBagConstraints);

        panel4.setLayout(new java.awt.GridBagLayout());

        btnMvTop.setEnabled(false);
        btnMvTop.setLabel("top");
        btnMvTop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMvTopActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        panel4.add(btnMvTop, gridBagConstraints);

        btnMvUp.setEnabled(false);
        btnMvUp.setLabel("up");
        btnMvUp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMvUpActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        panel4.add(btnMvUp, gridBagConstraints);

        btnMvDown.setEnabled(false);
        btnMvDown.setLabel("down");
        btnMvDown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMvDownActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        panel4.add(btnMvDown, gridBagConstraints);

        btnMvBottom.setEnabled(false);
        btnMvBottom.setLabel("bottom");
        btnMvBottom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMvBottomActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        panel4.add(btnMvBottom, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 4);
        add(panel4, gridBagConstraints);

        panel5.setLayout(new java.awt.GridBagLayout());

        btnCreateStack.setLabel("Create Stack");
        btnCreateStack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateStackActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        panel5.add(btnCreateStack, gridBagConstraints);

        btnCancel.setLabel("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        panel5.add(btnCancel, gridBagConstraints);

        btnHelp.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
        btnHelp.setLabel("Help");
        btnHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHelpActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        panel5.add(btnHelp, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        add(panel5, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        this.dispose();
    }//GEN-LAST:event_exitForm

private void btnMvRightAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMvRightAllActionPerformed
    for (String item : listImgOpen.getItems()) {
        listImgSelected.add(item);
        listImgOpen.remove(item);
    }

    if (listImgSelected.getItemCount() > 0) {
        btnMvLeft.setEnabled(true);
        btnMvLeftAll.setEnabled(true);
        btnMvUp.setEnabled(true);
        btnMvDown.setEnabled(true);
        btnMvTop.setEnabled(true);
        btnMvBottom.setEnabled(true);
    }
    if (listImgOpen.getItemCount() == 0) {
        btnMvRight.setEnabled(false);
        btnMvRightAll.setEnabled(false);
        listImgSelected.requestFocus();
    }
}//GEN-LAST:event_btnMvRightAllActionPerformed

private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
    this.dispose();
}//GEN-LAST:event_btnCancelActionPerformed

private void btnCreateStackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateStackActionPerformed
    if (listImgSelected.getItemCount() == 0) {
        IJ.error("No items for stack building selected!");
        return;
    }

    if (listImgSelected.getItemCount() < 2) {
        IJ.error("You need to select at least two items to build a new stack!");
        return;
    }

    int stackSize = WindowManager.getImage(listImgSelected.getItem(0)).getImageStackSize();
    for (String i : listImgSelected.getItems()) {
        int tempStackSize = WindowManager.getImage(i).getImageStackSize();
        if (tempStackSize != stackSize) {
            IJ.error("Selected items do not have equal slice count!");
            return;
        }
    }
    
    int width = WindowManager.getImage(listImgSelected.getItem(0)).getWidth();
    int height = WindowManager.getImage(listImgSelected.getItem(0)).getHeight();
    int length = WindowManager.getImage(listImgSelected.getItem(0)).getStackSize();

    ImageStack newStack = new ImageStack(width, height);

    for (int i = 1; i <= length; i++) {
        for (String j : listImgSelected.getItems()) {
            newStack.addSlice(j + " " + Integer.toString(i), WindowManager.getImage(j).getImageStack().getPixels(i));
        }
    }

    ImagePlus newImage = new ImagePlus("HDR Stack", newStack);
    newImage.show();

}//GEN-LAST:event_btnCreateStackActionPerformed

private void btnMvTopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMvTopActionPerformed
    int item = listImgSelected.getSelectedIndex();

    if (item == 0) {
        return;
    }

    String tempItem1 = listImgSelected.getItem(0);
    String tempItem2 = listImgSelected.getItem(item);
    listImgSelected.replaceItem(tempItem1, item);
    listImgSelected.replaceItem(tempItem2, 0);
    listImgSelected.select(0);
}//GEN-LAST:event_btnMvTopActionPerformed

private void btnMvDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMvDownActionPerformed
    int item = listImgSelected.getSelectedIndex();
    if (item + 1 >= listImgSelected.getItemCount()) {
        return;
    }

    String tempItem1 = listImgSelected.getItem(item + 1);
    String tempItem2 = listImgSelected.getItem(item);
    listImgSelected.replaceItem(tempItem1, item);
    listImgSelected.replaceItem(tempItem2, item + 1);
    listImgSelected.select(item + 1);
}//GEN-LAST:event_btnMvDownActionPerformed

private void btnMvLeftAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMvLeftAllActionPerformed
    for (String item : listImgSelected.getItems()) {
        listImgOpen.add(item);
        listImgSelected.remove(item);
    }

    if (listImgOpen.getItemCount() > 0) {
        btnMvRight.setEnabled(true);
        btnMvRightAll.setEnabled(true);
    }

    if (listImgSelected.getItemCount() == 0) {
        btnMvLeft.setEnabled(false);
        btnMvLeftAll.setEnabled(false);
        btnMvUp.setEnabled(false);
        btnMvDown.setEnabled(false);
        btnMvTop.setEnabled(false);
        btnMvBottom.setEnabled(false);
        listImgOpen.requestFocus();
    }


}//GEN-LAST:event_btnMvLeftAllActionPerformed

private void btnMvRightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMvRightActionPerformed
    for (String item : listImgOpen.getSelectedItems()) {
        listImgSelected.add(item);
        listImgOpen.remove(item);
    }

    if (listImgSelected.getItemCount() > 0) {
        btnMvLeft.setEnabled(true);
        btnMvLeftAll.setEnabled(true);
        btnMvUp.setEnabled(true);
        btnMvDown.setEnabled(true);
        btnMvTop.setEnabled(true);
        btnMvBottom.setEnabled(true);
    }
    if (listImgOpen.getItemCount() == 0) {
        btnMvRight.setEnabled(false);
        btnMvRightAll.setEnabled(false);
        listImgSelected.requestFocus();
    } else {
        listImgOpen.requestFocus();
    }
}//GEN-LAST:event_btnMvRightActionPerformed

private void btnMvLeftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMvLeftActionPerformed
    for (String item : listImgSelected.getSelectedItems()) {
        listImgOpen.add(item);
        listImgSelected.remove(item);
    }

    if (listImgOpen.getItemCount() > 0) {
        btnMvRight.setEnabled(true);
        btnMvRightAll.setEnabled(true);
    }

    if (listImgSelected.getItemCount() == 0) {
        btnMvLeft.setEnabled(false);
        btnMvLeftAll.setEnabled(false);
        btnMvUp.setEnabled(false);
        btnMvDown.setEnabled(false);
        btnMvTop.setEnabled(false);
        btnMvBottom.setEnabled(false);
        listImgOpen.requestFocus();
    } else {
        listImgSelected.requestFocus();
    }
}//GEN-LAST:event_btnMvLeftActionPerformed

private void btnMvUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMvUpActionPerformed
    int item = listImgSelected.getSelectedIndex();
    if (item - 1 < 0) {
        return;
    }

    String tempItem1 = listImgSelected.getItem(item - 1);
    String tempItem2 = listImgSelected.getItem(item);
    listImgSelected.replaceItem(tempItem1, item);
    listImgSelected.replaceItem(tempItem2, item - 1);
    listImgSelected.select(item - 1);
}//GEN-LAST:event_btnMvUpActionPerformed

private void btnMvBottomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMvBottomActionPerformed
    int item = listImgSelected.getSelectedIndex();

    if (item == listImgSelected.getItemCount() - 1) {
        return;
    }

    String tempItem1 = listImgSelected.getItem(listImgSelected.getItemCount() - 1);
    String tempItem2 = listImgSelected.getItem(item);
    listImgSelected.replaceItem(tempItem1, item);
    listImgSelected.replaceItem(tempItem2, listImgSelected.getItemCount() - 1);
    listImgSelected.select(listImgSelected.getItemCount() - 1);
}//GEN-LAST:event_btnMvBottomActionPerformed

private void btnHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHelpActionPerformed
 String helpMessage = "huhu2";
 TextWindow help = new TextWindow("H22elp",helpMessage,400,400);
 help.setVisible(true);
}//GEN-LAST:event_btnHelpActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new HDRStackBuilderFrame().setVisible(true);
            }
        });
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Button btnCancel;
    private java.awt.Button btnCreateStack;
    private java.awt.Button btnHelp;
    private java.awt.Button btnMvBottom;
    private java.awt.Button btnMvDown;
    private java.awt.Button btnMvLeft;
    private java.awt.Button btnMvLeftAll;
    private java.awt.Button btnMvRight;
    private java.awt.Button btnMvRightAll;
    private java.awt.Button btnMvTop;
    private java.awt.Button btnMvUp;
    private java.awt.Label lblImgOpen;
    private java.awt.Label lblImgSelected;
    private javax.swing.JLabel lblLogo;
    private java.awt.List listImgOpen;
    private java.awt.List listImgSelected;
    private java.awt.Panel panel1;
    private java.awt.Panel panel2;
    private java.awt.Panel panel3;
    private java.awt.Panel panel4;
    private java.awt.Panel panel5;
    // End of variables declaration//GEN-END:variables

}
